package com.bankcomm.ui.views;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.app.Activity;
import android.widget.RelativeLayout;

/**
 *仿SlidingMenu菜单控件
 *
 *@add by cheng.l 2013.05.21
 * */
public class SlidingMenu extends RelativeLayout {

	/**
	 * 可以拖动的view
	 * */
	private SlidingView mSlidingView;
	/**
	 * menu也就是左侧的View
	 * */
	private View mMenuView;
	/**
	 * mDetailView也就是右侧的View
	 * */
	private View mDetailView;

	/**
	 * 两边菜单设为屏幕宽度的3/4
	 * */
	int w = 0;
	
	public SlidingMenu(Context context) {
		super(context);
		init(context);
	}

	public SlidingMenu(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public SlidingMenu(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	private void init(Context context) {
		// TODO Auto-generated method stub
		DisplayMetrics dm = new DisplayMetrics();
		((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenWidth = dm.widthPixels;
		w = screenWidth*3/4;
	}
	
	/**
	 * 添加View，左、中、右
	 * */
	public void addViews(View left, View center, View right) {
		setLeftView(left);
		setRightView(right);
		setCenterView(center);
	}

	/**
	 * 添加View，左
	 * */
	public void setLeftView(View view) {
		LayoutParams behindParams = new LayoutParams(w,LayoutParams.FILL_PARENT);
		addView(view, behindParams);
		mMenuView = view;
	}
	
	/**
	 * 添加View，右
	 * */
	public void setRightView(View view) {
		LayoutParams behindParams = new LayoutParams(w,LayoutParams.FILL_PARENT);
		behindParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		addView(view, behindParams);
		mDetailView = view;
	}
	
	/**
	 * 添加View，中
	 * */
	public void setCenterView(View view) {
		LayoutParams aboveParams = new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT);
		mSlidingView = new SlidingView(getContext());
		addView(mSlidingView, aboveParams);
		mSlidingView.setView(view);
		mSlidingView.invalidate();
		mSlidingView.setMenuView(mMenuView);
		mSlidingView.setDetailView(mDetailView);
	}

	/**
	 * 显示View，左
	 * */
	public void showLeftView() {
		mMenuView.setVisibility(View.VISIBLE);
		mDetailView.setVisibility(View.INVISIBLE);
		mSlidingView.showLeftView();
	}

	/**
	 * 显示View，右
	 * */
	public void showRightView() {
		mDetailView.setVisibility(View.VISIBLE);
		mMenuView.setVisibility(View.INVISIBLE);
		mSlidingView.showRightView();
	}

	public void setMenuListener(OnMenuListener listener){
		mSlidingView.setMenuListener(listener);
	}
	
	/**
	 * SlidingMenu菜单事件
	 * */
	public interface OnMenuListener{
		public void openMenu(boolean right);
		public void closeMenu(boolean right);
	}
}
